package com.gexin.model.activity;

import com.gexin.common.utils.SQLUtils;
import com.gexin.common.utils.StringUtil;
import com.gexin.model.activity.base.BaseCoupon;
import com.jfinal.plugin.activerecord.Page;

/**
 * 业务名称:  t_coupon(优惠券表) 
 */
@SuppressWarnings("serial")
public class Coupon extends BaseCoupon<Coupon> {
	public static final Coupon dao = new Coupon();

//	10店铺优惠券 11新人店铺券  20商品优惠券  30类目优惠券
	public static final Integer USETYPE_ALL=10;
	public static final Integer USETYPE_NEWUSER=11;
	public static final Integer USETYPE_PRODUCT=20;
	public static final Integer USETYPE_PRODUCTTYPE=30;


	public Page<Coupon> findList(int page , int pageSize) {
		SQLUtils sql = new SQLUtils(" from t_coupon");
		return paginate(page,pageSize,"select * ",sql.toString());
	}

	/**
	 * 获取用户领取的优惠券
	 * @param pageIndex
	 * @param pageSize
	 * @param userId 用户id
	 * @param state
	 * @param type
	 */
	public Page<Coupon> findAllListByUserId(int pageIndex , int pageSize , String openId , String state , String type) {
		SQLUtils sql = new SQLUtils(" from t_user_coupon usercoupon left join t_coupon coupon on coupon.id=usercoupon.coupon_no  ");
		sql.append(" where usercoupon.user_id=? ");
		if(!StringUtil.isBlank(state)){
			sql.whereEquals("usercoupon.use_state",state);
		}
		if(!StringUtil.isBlank(type)){
			sql.whereEquals("usercoupon.type",type);
		}
		return paginate(pageIndex,pageSize,"select usercoupon.*,coupon.title as title ",sql.toString(),openId);
	}

	public void findAvailableCouponList(String productIds) {

	}
}
